import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.eclipse.swt.widgets.TableItem;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;

public class ItemRules {
	private static final String MARK_O = "○";
	String name;
	String fieldName;
	int maxLength;
	int minLength;
	boolean byte1;
	boolean byte2;
	boolean upperCase;
	boolean lowerCase;
	boolean numeric;
	String label;
	int kind;
	int capitalType;

	public String getName() {
		return name;
	}

	public String getFieldName() {
		return fieldName;
	}

	public int getMaxLength() {
		return maxLength;
	}

	public int getMinLength() {
		return minLength;
	}

	public boolean isByte1() {
		return byte1;
	}

	public boolean isByte2() {
		return byte2;
	}

	public boolean isUpperCase() {
		return upperCase;
	}

	public boolean isLowerCase() {
		return lowerCase;
	}

	public boolean isNumeric() {
		return numeric;
	}

	public String getLabel() {
		return label;
	}

	public void setLabel(String label) {
		this.label = label;
	}

	public int getKind() {
		return kind;
	}

	public int getCapitalType() {
		return capitalType;
	}

	ItemRules() {

	}

	ItemRules(HSSFRow row) {
		fieldName = row.getCell(2).getRichStringCellValue().toString();
		maxLength = -1;
		if (row.getCell(3).getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
			maxLength = (int) row.getCell(3).getNumericCellValue();
		}
		minLength = 0;
		if (row.getCell(4).getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
			minLength = (int) row.getCell(4).getNumericCellValue();
		}
		byte2 = row.getCell(5).getRichStringCellValue().toString().equals(
				MARK_O);
		byte1 = row.getCell(6).getRichStringCellValue().toString().equals(
				MARK_O);
		upperCase = row.getCell(7).getRichStringCellValue().toString().equals(
				MARK_O);
		lowerCase = row.getCell(8).getRichStringCellValue().toString().equals(
				MARK_O);
		numeric = row.getCell(9).getRichStringCellValue().toString().equals(
				MARK_O);
		label = row.getCell(10).getRichStringCellValue().toString();
		if (byte2) {
			kind = 0;
		} else if (numeric) {
			kind = 1;
		} else {
			kind = 2;
		}
		if (upperCase && lowerCase) {
			capitalType = 2;
		} else if (!upperCase && lowerCase) {
			capitalType = 1;
		} else {
			capitalType = 0;
		}
	}

	ItemRules(Node node) {
		name = node.getNodeName();
		NamedNodeMap nodeMap = node.getAttributes();
		fieldName = nodeMap.getNamedItem(PropertyClass.id).getNodeValue();
		Node tmpNode = nodeMap.getNamedItem(PropertyClass.maxlen);
		maxLength = (tmpNode == null) ? -1 : Integer.valueOf(
				tmpNode.getNodeValue()).intValue();
		tmpNode = nodeMap.getNamedItem(PropertyClass.minlen);
		minLength = (tmpNode == null) ? -1 : Integer.valueOf(
				tmpNode.getNodeValue()).intValue();
		tmpNode = nodeMap.getNamedItem(PropertyClass.kind);
		byte1 = true;
		if (tmpNode == null) {
			byte2 = true;
			numeric = true;
		} else {
			byte2 = false;
			if (tmpNode.getNodeValue().equals("1")) {
				numeric = true;
			} else {
				numeric = false;
			}
		}
		tmpNode = nodeMap.getNamedItem(PropertyClass.capitaltype);
		if (tmpNode == null) {
			upperCase = true;
			lowerCase = false;
		} else {
			if (tmpNode.getNodeValue().equals("1")) {
				upperCase = false;
				lowerCase = true;
			} else {
				upperCase = true;
				lowerCase = true;
			}
		}
		if (byte2) {
			kind = 0;
		} else if (numeric) {
			kind = 1;
		} else {
			kind = 2;
		}
		if (upperCase && lowerCase) {
			capitalType = 2;
		} else if (!upperCase && lowerCase) {
			capitalType = 1;
		} else {
			capitalType = 0;
		}
	}

	ItemRules(TableItem item) {
		for (int j = 0; j < item.getParent().getColumnCount(); j++) {
			String property = item.getParent().getColumn(j).getText();
			if (property.equals(PropertyClass.id)) {
				fieldName = item.getText(j);
			} else if (property.equals(PropertyClass.maxlen)) {
				if (item.getText(j) == null || item.getText(j) == "") {
					maxLength = -1;
				} else {
					maxLength = Integer.parseInt(item.getText(j));
				}
			} else if (property.equals(PropertyClass.minlen)) {
				if (item.getText(j) == null || item.getText(j) == "") {
					minLength = -1;
				} else {
					minLength = Integer.parseInt(item.getText(j));
				}
			} else if (property.equals(PropertyClass.kind)) {
				if (item.getText(j) == null || item.getText(j) == "") {
					kind = 0;
				} else {
					kind = Integer.parseInt(item.getText(j));
				}
			} else if (property.equals(PropertyClass.capitaltype)) {
				if (item.getText(j) == null || item.getText(j) == "") {
					capitalType = 0;
				} else {
					capitalType = Integer.parseInt(item.getText(j));
				}
			} else if (property.equals(PropertyClass.data)) {
				label = item.getText(j);
			} else if (property.equals(PropertyClass.header)
					&& item.getText(j).length() > 0) {
				label = item.getText(j);
			}
		}
	}

	public String toString() {
		return fieldName; /*
						 * + "-" + maxLength + "-" + minLength + "-" + byte2 +
						 * "-" + byte1 + "-" + upperCase + "-" + lowerCase + "-"
						 * + numeric + "-" + label;
						 */
	}
}
